package com.company.OfferGuide;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class 有效的括号 {

    public static void main(String[] args) {
        System.out.println(isValid("(])"));
    }

    public static boolean isValid(String s) {
        Map<Character,Character> map = new HashMap<>();
        map.put('}','{');
        map.put(']','[');
        map.put(')','(');
        Stack<Character> stack = new Stack<>();

        int m = s.length();

        for (int i = 0; i < m; i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                if (stack.isEmpty()) return false;
                if (stack.peek() == map.get(c)) {
                  stack.pop();
                } else {
                    return false;
                }
            } else {
                stack.push(c);
            }

        }

        return stack.isEmpty();
    }

}
